﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNET4Study
{
    class ConvariantContravariant3
    {
        public static void Exectue()
        {
            IOutMyList<Student> studentList = new OutMyList<Student>();
            IOutMyList<Person> personList = studentList;
            personList.GetElement();

            IInMyList<Person> myAnimals = new InMyList<Person>();
            IInMyList<Student> myDogs = myAnimals;
            myDogs.ChangeT(new Student());
        }
    }

    #region 1.协变演示
    public interface IOutMyList<out T>
    {
        T GetElement();

        // 协变的参数不能作为输入参数在方法中使用
        //void ChangeT(T t); 
    }

    public class OutMyList<T> : IOutMyList<T>
    {
        public T GetElement()
        {
            return default(T);
        }

        // 协变的参数不能作为输入参数在方法中使用
        //public void ChangeT(T t)
        //{
        //    //Change T
        //} 
    }
    #endregion

    #region 2.逆变演示
    public interface IInMyList<in T>
    {
        void ChangeT(T t);
    }

    public class InMyList<T> : IInMyList<T>
    {
        public void ChangeT(T t)
        {
            //Change T
        }
    } 
    #endregion
}
